<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <!-- <el-form-item label="用户标识—外键" prop="articleUserId">
        <el-input
          v-model="queryParams.articleUserId"
          placeholder="请输入用户标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章内容" prop="articleContent">
        <el-input
          v-model="queryParams.articleContent"
          placeholder="请输入文章内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文章时间" prop="articleTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.articleTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择文章时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="转发量" prop="articleTransmitCounts">
        <el-input
          v-model="queryParams.articleTransmitCounts"
          placeholder="请输入转发量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="文章类型" prop="articleTypes">
        <!-- <el-input
          v-model="queryParams.articleTypes"
          placeholder="请输入文章类型 0平台 1车主"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.articleTypes" placeholder="请选择文章类型" clearable size="small">
          <el-option :value="0">平台</el-option>
          <el-option :value="1">车主</el-option>
        </el-select>
      </el-form-item><!--
      <el-form-item label="关联标识—外键" prop="articleRelevanceId">
        <el-input
          v-model="queryParams.articleRelevanceId"
          placeholder="请输入关联标识—外键"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="浏览量" prop="articlePageView">
        <el-input
          v-model="queryParams.articlePageView"
          placeholder="请输入浏览量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="文章状态" prop="articleStatus">
        <el-select v-model="queryParams.articleStatus" placeholder="请选择文章状态" clearable size="small">
          <el-option label="正常" :value="0" />
          <el-option label="违规" :value="1" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:article:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:article:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:article:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:article:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="articleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" /><!--
      <el-table-column label="文章标识" align="center" prop="articleId" /> -->
      <el-table-column label="用户姓名" align="center" prop="articleUserId" >
        <template slot-scope="scope">
          {{scope.row.personage.username}}
        </template>
      </el-table-column>
      <el-table-column label="文章内容" align="center" prop="articleContent" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{scope.row.articleContent}}
        </template>
      </el-table-column>

      <el-table-column label="文章时间" align="center" prop="articleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.articleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="转发量" align="center" prop="articleTransmitCounts" >
        <template slot-scope="scope">
          {{scope.row.articleTransmitCounts != null ? scope.row.articleTransmitCounts : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="文章类型" align="center" prop="articleTypes" >
        <template slot-scope="scope">
          {{scope.row.articleTypes == 0 ? '平台':'车主' }}
        </template>
      </el-table-column>
      <el-table-column label="关联商品" align="center" prop="articleRelevanceId" >
        <template slot-scope="scope">
          {{scope.row.relevance != null ? scope.row.relevance.goods.goodsName: '暂无'}}
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="articlePageView" >
        <template slot-scope="scope">
          {{scope.row.articlePageView != null ? scope.row.articlePageView : '0'}}
        </template>
      </el-table-column>
      <el-table-column label="文章状态" align="center" prop="articleStatus" >
        <template slot-scope="scope">
          {{scope.row.articleStatus == 0 ? '正常' : '违规'}}<!-- 0正常 1违规  -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:article:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:article:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文章对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户标识" prop="articleUserId">
          <el-select v-model="form.articleUserId" placeholder="请选择用户" style="width: 100%">
            <el-option v-for="(item,index) in personage" :key = "index" :value="item.uid" :label="item.username"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleContent">
          <el-input type="textarea" v-model="form.articleContent" placeholder="请输入文章内容" />
        </el-form-item>
        <el-form-item label="文章时间" prop="articleTime">
          <el-date-picker clearable size="small" style="width: 100%"
            v-model="form.articleTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择文章时间">
          </el-date-picker>
        </el-form-item><!--
        <el-form-item label="转发量" prop="articleTransmitCounts">
          <el-input v-model="form.articleTransmitCounts" placeholder="请输入转发量" />
        </el-form-item> -->
        <el-form-item label="文章类型" prop="articleTypes">
          <!-- <el-radio-group v-model="form.articleTypes">
            <el-radio :value="0">平台</el-radio>
            <el-radio :value="1">车主</el-radio>
          </el-radio-group> -->
          <el-select v-model="form.articleTypes" style="width: 100%;" placeholder="请选择文章类型">
            <el-option :value="0" label="平台"></el-option>
            <el-option :value="1" label="车主"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关联商品" prop="articleRelevanceId">
          <el-select v-model="form.articleRelevanceId" placeholder="请选择关联商品" style="width: 100%;">
            <el-option v-for="(items,indexs) in goodsList" :key = "indexs" :value="items.goodsId" :label="items.goodsName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="文章状态" prop="articleStatus">
          <el-select v-model="form.articleStatus" style="width: 100%;" placeholder="请选择文章状态">
            <el-option :value="0" label="正常"></el-option>
            <el-option :value="1" label="违规"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listArticle, getArticle, delArticle, addArticle, updateArticle, exportArticle } from "@/api/system/article";
import { listPersonage } from "@/api/system/personage";
import { listGoods } from "@/api/system/goods";
export default {
  name: "Article",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 文章表格数据
      articleList: [],
      personage: [],
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        articleUserId: undefined,
        articleContent: undefined,
        articleTime: undefined,
        articleTransmitCounts: undefined,
        articleTypes: undefined,
        articleRelevanceId: undefined,
        articlePageView: undefined,
        articleStatus: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        articleUserId: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" }
        ],
        articleContent: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ],
        articleTime: [
          { required: true, message: "文章时间不能为空", trigger: "blur" }
        ],
        articleTransmitCounts: [
          { required: true, message: "转发量不能为空", trigger: "blur" }
        ],
        articleTypes: [
          { required: true, message: "文章类型不能为空", trigger: "blur" }
        ],
        articleRelevanceId: [
          { required: true, message: "关联商品不能为空", trigger: "blur" }
        ],
        articlePageView: [
          { required: true, message: "浏览量不能为空", trigger: "blur" }
        ],
        articleStatus: [
          { required: true, message: "文章状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getPersonageList();
    this.getGoodsList();
  },
  methods: {
    getGoodsList(){
      listGoods().then(response =>{
        this.goodsList = response.rows;
      })
    },
    getPersonageList(){
      listPersonage().then(response =>{
        this.personage = response.rows;
      })
    },
    /** 查询文章列表 */
    getList() {
      this.loading = true;
      listArticle(this.queryParams).then(response => {
        this.articleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        articleId: undefined,
        articleUserId: undefined,
        articleContent: undefined,
        articleTime: undefined,
        articleTransmitCounts: undefined,
        articleTypes: undefined,
        articleRelevanceId: undefined,
        articlePageView: undefined,
        articleStatus: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.articleId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文章";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const articleId = row.articleId || this.ids
      getArticle(articleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文章";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.articleId != undefined) {
            updateArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addArticle(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const articleIds = row.articleId || this.ids;
      this.$confirm('是否确认删除文章编号为"' + articleIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delArticle(articleIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有文章数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportArticle(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
